package c926.xiaoduyun.xiaodu.mservice;

import kd.bos.base.AbstractBasePlugIn;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.control.EntryGrid;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.EventObject;

/**
 * 消毒方案插件
 */
public class XiaoduyunBaseDataPlugin extends AbstractBasePlugIn implements Plugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        System.out.println("========进入插件=============");
        super.afterCreateNewData(e);
        // 定义查询参数
        String entityName = "c926_level";
        String fields = "number,name,masterid";
        QFilter[] filters = new QFilter[]{};
        // 加载数据
        DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load(entityName, fields, filters);
        // 处理查询结果
        if (null != dynamicObjects) {
            for (DynamicObject baseData : dynamicObjects) {
                String name = baseData.getString("name");
                Long masterId = (Long) baseData.get("masterid");
                // 设置模型值和行背景颜色
                setDisinfectionLevel(masterId, name);
            }
        }
    }

    // 设置消毒级别及对应行颜色
    private void setDisinfectionLevel(Long masterId, String levelName) {
        EntryGrid levelGrid;
        String color;
        int index;
        switch (levelName) {
            case "初级消毒":
                levelGrid = this.getView().getControl("c926_levelentity");
                color = "#00FF00";
                index = 0;
                break;
            case "中级消毒":
                levelGrid = this.getView().getControl("c926_levelentity");
                color = "#FFFF00";
                index = 1;
                break;
            case "深度消毒":
                levelGrid = this.getView().getControl("c926_levelentity");
                color = "#FF0000";
                index = 2;
                break;
            default:
                return; // 如果不是预期的消毒级别，直接返回
        }
        levelGrid.setRowBackcolor(color, new int[]{index});
        this.getModel().setValue("c926_level", masterId, index);
    }
}